package cn.net.cfss.fgbp.face.manage.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wutao
 * @version 1.0
 * @date 2020/4/30 17:40
 */
@Service
@Slf4j
public class FormatUtil {



    @Value("${ip.regular.expression:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])}")
    private String ipRegular;

    @Value("${port.regular.expression:([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])}")
    private String portRegular;

    @Value("${extensionnum.regular.expression:[0-9]{1,5}}")
    private String extensionNumRegulat;

    @Value("${prefix.regular.expression:[3][1-2][0-9][0-9][0-9]}")
    private String prefixRegulat;

    @Value("${prefix.linegroup.regular.expression:[3][3-4][0-9][0-9][0-9]}")
    private String linegroupPrefixRegulat;

    @Value("${ring.format:wav}")
    private String ringFormat;

    @Value("${line.areacode.regular.expression:[0-9]{1,10}}")
    private String areaCodeRegulat;

    public boolean isIp(String ip) {
        return checkReg(ip, ipRegular);
    }

    public boolean isPort(String str) {
        return checkReg(str, portRegular);
    }

    public boolean isExtensionNum(String str) {
        return checkReg(str, extensionNumRegulat);
    }

    public Boolean isPrefix(String str) {
        return checkReg(str, prefixRegulat);
    }

    public Boolean isLinegroupPrefix(String str) {
        return checkReg(str, linegroupPrefixRegulat);
    }

    public Boolean isLineAreacode(String str) {
        return checkReg(str, areaCodeRegulat);
    }

    private boolean checkReg(String val,String regex)
    {
        if(StringUtils.isBlank(val)) {
            return false;
        }
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(val);
        return m.matches();
    }

    /**
     * 校验铃声格式
     * @param fileExtName 文件拓展名
     * @return
     */
    public boolean checkRingFormat(String fileExtName) {
        String[] split = ringFormat.split(",");
        for (String s : split) {
            if (s.equalsIgnoreCase(fileExtName)) {
                return true;
            }
        }
        return false;
    }












}
